package com.codefish.framework.spring.aop.framework.adapter;

import com.codefish.framework.spring.aop.IAfterAdvice;
import com.codefish.framework.spring.aop.IAfterReturningAdvice;
import com.codefish.framework.spring.core.utils.AssertUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 实现方法正常返回调用拦截
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/21 下午 03:30
 */
public class AfterReturningInterceptor implements MethodInterceptor, IAfterAdvice {
    IAfterReturningAdvice advice;

    public AfterReturningInterceptor(IAfterReturningAdvice afterReturningAdvice) {
        AssertUtils.notNull(afterReturningAdvice, "afterReturningAdvice不能为空");
        this.advice = afterReturningAdvice;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object retVal = invocation.proceed();
        advice.afterReturn(retVal, invocation.getMethod(), invocation.getArguments(), invocation.getThis());
        return retVal;
    }
}
